<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Hash;
class UserController extends Controller
{
   // 前台用户个人信息
   public function getIndex(Request $request){
      if(session('web') == 2){
        // 关闭状态
        $webconfig = DB::table('webconfig') -> first();
        return view('home/webconfig/index',['webconfig'=> $webconfig]);
       }else{
            $id = $request -> input('id'); 
    // // 查询个人信息数据
    $data = DB::table('user') ->where('id',$id)-> first();

    return view('home/user/index',['data'=>$data]);
       }
 
   }  
   // 验证进行修改
   public function postUpdate(Request $request){
    // 获取名字  id 
    $res = $request -> only('username');

    $old = $request -> only('oldpassword');
    $id = $request -> input('id');

     // 通过id 验证原密码
    $data = DB::table('user') -> where('id',$id) -> first();

     // 哈希解密
   if (Hash::check($old['oldpassword'], $data['password']))
{  
   // 验证成功 进行表单验证  数据修改
    $this->validate($request,[
        'password'=>'required',
        'repassword'=>'required|same:password',
        ],[
        'password.required'=>'请输入密码',
        'repassword.required'=>'请输入确认密码',
        'repassword.same'=>'确认密码不一致',
        ]);
     
    // 接受密码
    $password = $request -> input('password');
    
     // 进行哈希加密
    $res['password'] = Hash::make($password);
     // dd($data['id']);
    // 修改
    $datas = DB::table('user') ->where('id',$data['id'])->update($res);
   
    if($datas = 1){
       return redirect('/home') -> with('id',$data['id']);
    }else{
        return back() -> with('error','修改失败');
    }
        
}else{
    return back() -> with('error','原密码错误');
}
   
   }
   // 退出登录
    public function getTuichu(Request $request){
      $request->session()->forget('id');

    return redirect('/home');
   } 
}
